package com.diy.sigmund.diyorder.controller;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.entity.enumerate.VerifyParamPolicyEnum;
import com.diy.sigmund.diycommon.spring.anno.IgnoreTokenVerification;
import com.diy.sigmund.diycommon.spring.anno.VerifyParam;
import com.diy.sigmund.diycommon.spring.anno.VerifyParams;
import com.diy.sigmund.diyorder.entity.dto.OdSeckillOrderDTO;
import com.diy.sigmund.diyorder.entity.dto.TestBetweenAndDTO;
import com.diy.sigmund.diyorder.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ylm-sigmund
 * @since 2021/11/3 22:05
 */
@RestController
@RequestMapping("/order/secKill")
public class SecKillController {

    @Autowired
    private SecKillService secKillService;

    /**
     * 抢购
     *
     * @param odSeckillOrderDTO odSeckillOrderDTO
     * @return ResultMsg
     */
    @PostMapping("/panicBuying")
    @VerifyParams(value = {
            @VerifyParam(fieldName = "commodityId", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "商品id不能为空"),
            @VerifyParam(fieldName = "transactionQuantity", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "交易数量不能为空"),
            @VerifyParam(fieldName = "transactionQuantity", verifyPolicy = VerifyParamPolicyEnum.MAX, verifyValue = "1"),})
    public ResultMsg panicBuying(@RequestBody OdSeckillOrderDTO odSeckillOrderDTO) {
        // 下单前置参数校验，使用入参校验拦截器
        return secKillService.panicBuying(odSeckillOrderDTO);
    }

    /**
     * 查询订单记录
     *
     * @param odSeckillOrderDTO odSeckillOrderDTO
     * @return ResultMsg
     */
    @PostMapping("/getOrder")
    @VerifyParams(value = {
            @VerifyParam(fieldName = "id", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "订单id不能为空"),})
    public ResultMsg getOrder(@RequestBody OdSeckillOrderDTO odSeckillOrderDTO) {
        return secKillService.getOrder(odSeckillOrderDTO);
    }

    @PostMapping("/decrOneInventory")
    @IgnoreTokenVerification
    public ResultMsg decrOneInventory() {
        return secKillService.decrOneInventory(null);
    }

    /**
     * <br>测试mysql BetweenAnd datetime 边界值。等价于 >=added_time and <=off_shelf_time
     * <br>mysql 的time,datetime,timestamp的精度可达微秒（6位）
     *
     * <br>数据库：added_time 2021-11-26 10:00:00,off_shelf_time 2021-11-26 12:59:59
     * <br>select * from od_seckill_commodity where id = '1001' and '2021-11-26 12:24:59' between added_time and off_shelf_time
     *
     * <br>查询有值：currentDateTime:20211126125958999 20211126125959000 20211126100000000 20211126100000001
     * <br>查询无值：currentDateTime:20211126125959001 20211126095959999
     *
     * @param testBetweenAndDTO testBetweenAndDTO
     * @return ResultMsg
     */
    @PostMapping("/testBetweenAnd")
    public ResultMsg testBetweenAnd(TestBetweenAndDTO testBetweenAndDTO) {
        return secKillService.testBetweenAnd(testBetweenAndDTO);
    }

}
